import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class LeagueManager {
    private ArrayList<Team> teams;

    public LeagueManager() {
        teams = new ArrayList<>();
    }

    public void addTeam(Team team) {
        teams.add(team);
    }

    public void recordMatchResult(String homeTeamName, String awayTeamName, int homeGoals, int awayGoals) {
        Team homeTeam = findTeamByName(homeTeamName);
        Team awayTeam = findTeamByName(awayTeamName);

        if (homeTeam != null && awayTeam != null) {
            homeTeam.updateMatch(homeGoals, awayGoals);
            awayTeam.updateMatch(awayGoals, homeGoals);
        } else {
            System.out.println("输入的队名无效。");
        }
    }

    private Team findTeamByName(String name) {
        for (Team team : teams) {
            if (team.getName().equalsIgnoreCase(name)) {
                return team;
            }
        }
        return null;
    }

    public void sortTeams() {
        Collections.sort(teams, new Comparator<Team>() {
            @Override
            public int compare(Team t1, Team t2) {
                if (t2.getPoints() != t1.getPoints()) {
                    return t2.getPoints() - t1.getPoints();
                } else if (t2.getGoalDifference() != t1.getGoalDifference()) {
                    return t2.getGoalDifference() - t1.getGoalDifference();
                } else {
                    return t2.getGoalsScored() - t1.getGoalsScored();
                }
            }
        });
    }

    public void displayStandings() {
        System.out.printf("%-15s %2s %2s %2s %2s %3s %3s %3s %3s\n", "球队名", "场次", "胜", "平", "负", "进球", "失球", "积分", "净胜球");
        for (Team team : teams) {
            System.out.println(team);
        }
    }

    public static void main(String[] args) {
        LeagueManager league = new LeagueManager();

        // 添加示例球队
        league.addTeam(new Team("A"));
        league.addTeam(new Team("B"));
        league.addTeam(new Team("C"));
        league.addTeam(new Team("D"));

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入比赛结果：格式为 主队名 客队名 主队进球 客队进球（例如：A B 2 1）\n退出输入 exit");

        while (true) {
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("exit")) break;

            String[] matchDetails = input.split(" ");
            if (matchDetails.length != 4) {
                System.out.println("输入格式错误，请重新输入！");
                continue;
            }

            String homeTeam = matchDetails[0];
            String awayTeam = matchDetails[1];
            int homeGoals;
            int awayGoals;

            try {
                homeGoals = Integer.parseInt(matchDetails[2]);
                awayGoals = Integer.parseInt(matchDetails[3]);
            } catch (NumberFormatException e) {
                System.out.println("进球数必须为数字，请重新输入！");
                continue;
            }

            league.recordMatchResult(homeTeam, awayTeam, homeGoals, awayGoals);
            league.sortTeams();
            league.displayStandings();
        }

        scanner.close();
    }
}
